//
//  ErrorTableViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/7.
//  Copyright © 2018年 longlong. All rights reserved.
//
NSString * const NSCustomErrorDomain = @"这是一个自定义的错误域";

#define CellIdentifer @"UITableViewCell"

#import "ErrorTableViewController.h"
#import "UIAlertController+HandleError.h"
#import <objc/runtime.h>

@interface ErrorTableViewController ()

@end

@implementation ErrorTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifer];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    
    if (indexPath.row == 0)
    {
        cell.textLabel.text = @"创建一个error";
    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = @"字符串错误error";
    }
    else if (indexPath.row == 2)
    {
        cell.textLabel.text = @"json 错误";
    }
    else if (indexPath.row == 3)
    {
        cell.textLabel.text = @"写数据";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSError *error;
    if (indexPath.row == 0)
    {
        NSDictionary *dict = @{NSLocalizedDescriptionKey:@"文件不能被写入",
                               NSLocalizedRecoverySuggestionErrorKey:@"你要再次写入嘛？",
                               NSLocalizedRecoveryOptionsErrorKey:@[@"取消",@"确定",@"想会"],
                               NSURLErrorFailingURLErrorKey:[NSURL URLWithString:@"https://www.baidu.com"]
                               };
        
        error = [NSError errorWithDomain:NSCustomErrorDomain code:909 userInfo:dict];
    }
    else if (indexPath.row == 1)
    {
        [NSString stringWithContentsOfFile:@"123" encoding:NSUTF8StringEncoding error:&error];
    }
    else if (indexPath.row == 2)
    {
    }
    else if (indexPath.row == 3)
    {

    }
    
    NSLog(@"error : \n%@",[self dictionaryRepresentationWithError:error]);

    

}

- (void)logErrorUserInfoKey
{
    NSLog(@"NSUnderlyingErrorKey = %@",NSUnderlyingErrorKey);
    NSLog(@"NSLocalizedDescriptionKey = %@",NSLocalizedDescriptionKey);
    NSLog(@"NSLocalizedFailureReasonErrorKey = %@",NSLocalizedFailureReasonErrorKey);
    NSLog(@"NSLocalizedRecoverySuggestionErrorKey = %@",NSLocalizedRecoverySuggestionErrorKey);
    NSLog(@"NSLocalizedRecoveryOptionsErrorKey = %@",NSLocalizedRecoveryOptionsErrorKey);
    NSLog(@"NSRecoveryAttempterErrorKey = %@",NSRecoveryAttempterErrorKey);
    NSLog(@"NSHelpAnchorErrorKey = %@",NSHelpAnchorErrorKey);
    NSLog(@"NSDebugDescriptionErrorKey = %@",NSDebugDescriptionErrorKey);
//    NSLog(@"NSLocalizedFailureErrorKey = %@",NSLocalizedFailureErrorKey);
    NSLog(@"NSStringEncodingErrorKey = %@",NSStringEncodingErrorKey);
    NSLog(@"NSURLErrorKey = %@",NSURLErrorKey);
    NSLog(@"NSFilePathErrorKey = %@",NSFilePathErrorKey);
    
}


- (NSDictionary *)dictionaryRepresentationWithError:(NSError *)error
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    unsigned int count = 0;
    Ivar *iavrList = class_copyIvarList(NSError.class, &count);
    for (int i = 0; i < count; i ++)
    {
        Ivar ivar = iavrList[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        if ([error respondsToSelector:@selector(valueForKey:)] && ![key containsString:@"reserved"])
        {
            if ([error valueForKey:key])
            {
                [mutableDict setValue:[error valueForKey:key] forKey:key];
            }
            else
            {
                [mutableDict setValue:[NSNull null] forKey:key];
            }
        }
    }
    free(iavrList);
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (void)showError:(NSError *)error
{
    UIAlertController *alertController = [UIAlertController showError:error ActionHandler:^(NSString *buttonTitle) {
        NSLog(@"buttonTitle === %@",buttonTitle);
    }];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
